package com.mrzhou.file.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.emc.object.s3.S3Client;
import com.emc.object.s3.S3Config;
import com.emc.object.s3.jersey.S3JerseyClient;
import com.mrzhou.file.props.FileAwsProperties;
import com.mrzhou.web.props.HttpClientPoolProperties;
import com.mrzhou.web.support.HttpClientConfig;
import com.sun.jersey.client.apache4.ApacheHttpClient4Handler;
import org.apache.http.impl.client.BasicCookieStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;
import java.net.URISyntaxException;

@Configuration
public class FileConfiguration {

    @Autowired
    private FileAwsProperties props;
    @Autowired
    private HttpClientPoolProperties poolProperties;

    @Bean
    public AmazonS3 amazonS3() {
        AWSCredentials credentials = new BasicAWSCredentials(props.getAccessKey(), props.getSecretKey());

        ClientConfiguration conf = new ClientConfiguration();
        conf.setMaxConnections(200); // 设置AmazonS3使用的最大连接数
        conf.setSocketTimeout(5000); // 设置socket超时时间, 单位: ms
        conf.setMaxErrorRetry(3); // 设置失败请求重试次数
        conf.setProtocol(Protocol.HTTPS); // 设置链接的访问协议
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(props.getEndpoint(), Regions.US_EAST_1.getName()))
                .withClientConfiguration(conf)
                .disableChunkedEncoding()
                .withPayloadSigningEnabled(true)
                .withPathStyleAccessEnabled(true)
                .withForceGlobalBucketAccessEnabled(true)
                .build();
//        checkAndCreateBucket(s3Client);
        return s3Client;
    }

    /**
     * 检查桶是否存在，不存在则创建创建
     * @param s3Client
     */
    private void checkAndCreateBucket(AmazonS3 s3Client){
        boolean exists = s3Client.doesBucketExistV2(props.getBucket());
        if (!exists){
            s3Client.createBucket(props.getBucket());
//            throw  new IllegalArgumentException(String.format("AWS不存在的桶【%s】", props.getBucket()));
        }
    }

    /* ECS S3配置 */
    @Bean
    public S3Client s3Client() throws URISyntaxException {
        S3Config config = new S3Config(new URI(props.getProtocol() + "://" + props.getEndpoint()));
        config.withIdentity(props.getAccessKey()).withSecretKey(props.getSecretKey());
        // 自定义HttpClient设置
        ApacheHttpClient4Handler handler = new ApacheHttpClient4Handler(HttpClientConfig.createHttpClient(poolProperties), new BasicCookieStore(), true);
        S3Client client = new S3JerseyClient(config, handler);
        return client;
    }

}
